package cn.ym.order.service.impl;

import cn.ym.basic.exception.BusinessException;
import cn.ym.basic.util.AjaxResult;
import cn.ym.basic.util.CodeGenerateUtils;
import cn.ym.basic.util.LoginContext;

import cn.ym.order.domain.OrderProduct;
import cn.ym.order.mapper.OrderProductMapper;
import cn.ym.order.service.IOrderProductService;
import cn.ym.basic.service.impl.BaseServiceImpl;

import cn.ym.pet.dto.OrderDto;
import cn.ym.product.domain.Product;
import cn.ym.product.mapper.ProductMapper;

import cn.ym.user.domain.Logininfo;
import cn.ym.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {

    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ProductMapper productMapper;



    /**
     * 确认订单
     * @param
     * @return
     */
    @Override
    public void confirm(OrderDto orderDto) {
        //1.将寻主消息放入寻主池：msg的state=3 and shopId = null
        OrderProduct orderProduct = orderProductMapper.findOne(orderDto.getId());

        //2.改订单的状态： 1
        orderProduct.setState(1);
        orderProduct.setPrice(orderDto.getMoney());
        orderProductMapper.update(orderProduct);
    }

    /**
     * 订单取消
     * @param id
     * @return
     */
    @Override
    public void cancel(Long id) {
        //1.将寻主消息放入寻主池：msg的state=3 and shopId = null
        OrderProduct orderProduct = orderProductMapper.findOne(id);
        //2.改订单的状态：-1
        orderProduct.setState(-1);
        orderProductMapper.update(orderProduct);
    }


    @Override
    public void submit(Map<String, String> map, HttpServletRequest request) {
        //获取前端传来的参数
        //送货方式
        String serviceMethod = map.get("service_method");
        //地址
        String addressId = map.get("address_id");
        //支付方式
        String payMethod = map.get("pay_method");
        //服务id
        Long productId = Long.valueOf(map.get("product_id"));

        //一：校验 - 空值校验
        if (StringUtils.isEmpty(serviceMethod) || StringUtils.isEmpty(addressId)
                || StringUtils.isEmpty(payMethod)){
            throw new BusinessException("请选择");
        }
        Logininfo logininfo = LoginContext.getLogininfo(request);

        Product product = productMapper.findOne(productId);
        //生成订单
        OrderProduct orderProduct = map2OrderProduct(logininfo,product);
        orderProduct.setProductId(productId);
        orderProductMapper.add(orderProduct);
    }


    private OrderProduct map2OrderProduct(Logininfo logininfo, Product product) {
        OrderProduct orderProduct = new OrderProduct();
        //摘要
        orderProduct.setDigest("[服务订单]购买"+product.getName()+"服务！");
        //状态
        orderProduct.setState(0);
        //价格
        orderProduct.setPrice(product.getSaleprice());
        //订单编号
        String orderSn = CodeGenerateUtils.generateOrderSn(logininfo.getId());
        orderProduct.setOrderSn(orderSn);
        //支付编号
        String paySn = CodeGenerateUtils.generateUnionPaySn();
        orderProduct.setPaySn(paySn);
        //3天之后没有接单就自动取消 - 定时器
        orderProduct.setLastPayTime(new Date(System.currentTimeMillis()+ 3*24*60*60*1000));
        orderProduct.setUserId(logininfo.getId());
        return orderProduct;
    }

}
